﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using System.IO.Pipes;
using System.Threading;
using Microsoft.Win32;
using Utilities;
using UILibrary;

namespace OfficeOnline
{
    internal class LicenseChecker
    {
        public bool isLicenseValid(DbOperations dbOperations)
        {
            bool disabledMode = true;

            try
            {
                RegistryKey kHKCU = Registry.CurrentUser;
                RegistryKey kFirstRun = kHKCU.OpenSubKey("Software\\UpriseApps\\OfficeOnline", true);
                if (kFirstRun == null)
                {
                    kFirstRun = kHKCU.CreateSubKey("Software\\UpriseApps\\OfficeOnline", RegistryKeyPermissionCheck.ReadWriteSubTree);
                    kFirstRun.SetValue("FirstRun", 1);
                }

                if (kFirstRun.GetValue("FirstRun").ToString().Equals("1"))
                {
                    //Registration Wizard
                    Registration regForm = new Registration(dbOperations);
                    if (DialogResult.OK == regForm.ShowDialog())
                    {
                        kFirstRun.SetValue("FirstRun", 0);
                        disabledMode = false;
                    }
                }
                else disabledMode = false;
            }
            catch (Exception ex)
            {
                Utilities.TraceWrapper.LogException("ThisAddIn_Startup error: " + ex.Message, ex);

                MessageBox.Show("Office In Cloud for Google Docs (Excel 2007) was not properly installed. \n Please contact Uprise Apps support.", "Office In Cloud Error Message");
            }

            return disabledMode;
        }
    }
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            CommonOperations.initPaths("\\Uprise Apps\\OfficeOnline\\OfficeOnline.txt", 
                                        "\\Uprise Apps\\OfficeOnline\\Info.mdb",
                                        System.Reflection.Assembly.GetEntryAssembly().GetName().CodeBase);

            TraceWrapper traceWrapper = new TraceWrapper();
            traceWrapper.initTraceListener();
            traceWrapper.traceSystemInfo(System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(),
                                         System.Reflection.Assembly.GetEntryAssembly().GetName().ToString());

            DbOperations dbOperations = new DbOperations(new DbManager(Utilities.Path.dbPath));

            //registerExtensions();

            string file= string.Empty;
            if (args.Length > 0)
                file = args[0];

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool disabledMode = new LicenseChecker().isLicenseValid(dbOperations);
            MdiForm parent = new MdiForm(file, dbOperations, traceWrapper, disabledMode);
            
            // if at least one application is already run and logged in, we get the credentials via named pipe
            //if (!parent.isSilientLoginPossible())
            //    parent.login();

            Application.Run(parent);
        }
    }
}
